<template>
  <div class="app-container">
    <div class="search-bar">
      <el-form ref="queryFormRef" :model="queryParams" :inline="true">
        <el-form-item label="用户名">
          <el-input
            v-model="queryParams.username"
            placeholder="请输入用户名"
            clearable
            style="width: 200px"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="手机号">
          <el-input
            v-model="queryParams.mobile"
            placeholder="请输入手机号"
            clearable
            style="width: 200px"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery">
            <template #icon><Search /></template>
            搜索
          </el-button>
          <el-button @click="handleResetQuery">
            <template #icon><Refresh /></template>
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never">
      <el-table
          ref="dataTableRef"
          v-loading="loading"
          :data="pageData"
          highlight-current-row
          border
      >
        <el-table-column key="id" label="ID" prop="id" min-width="80" align="center" />
        <el-table-column key="moneyOrderId" label="订单号" prop="moneyOrderId" min-width="180" align="center" />
        <el-table-column key="userId" label="用户ID" prop="userId" min-width="100" align="center" />
        <el-table-column key="nickname" label="用户昵称" prop="nickname" min-width="100" align="center" />
        <el-table-column key="money" label="金额" prop="money" min-width="100" align="center">
          <template #default="scope">
            {{ scope.row.money ? `¥${scope.row.money}` : '-' }}
          </template>
        </el-table-column>
        <el-table-column key="after" label="变动后余额" prop="after" min-width="120" align="center" />
        <el-table-column key="operationType" label="支付类型" prop="operationType" min-width="100" align="center">
          <template #default="scope">
            {{ getOperationTypeText(scope.row.operationType) }}
          </template>
        </el-table-column>
        <el-table-column key="operatorId" label="操作人ID" prop="operatorId" min-width="100" align="center" />
        <el-table-column key="operationTime" label="操作时间" prop="operationTime" min-width="160" align="center" />
        <el-table-column key="remark" label="备注" prop="remark" min-width="150" align="center" />
        <el-table-column key="payPic" label="支付凭证" prop="payPic" min-width="120" align="center">
          <template #default="scope">
            <el-image v-if="scope.row.payPic" :src="scope.row.payPic" :preview-src-list="[scope.row.payPic]" style="width:40px;height:40px;object-fit:cover" fit="cover" :preview-teleported="true" :initial-index="0" />
            <span v-else>-</span>
          </template>
        </el-table-column>
        <el-table-column key="auditStatus" label="审核状态" prop="auditStatus" min-width="100" align="center">
          <template #default="scope">
            <el-tag :type="getAuditStatusType(scope.row.auditStatus)">
              {{ getAuditStatusText(scope.row.auditStatus) }}
            </el-tag>
          </template>
        </el-table-column>
      </el-table>

      <pagination
          v-if="total > 0"
          v-model:total="total"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="handleQuery()"
      />
    </el-card>
  </div>
</template>

<script setup lang="ts">
import RechargeAPI from "@/api/genealogy/recharge.api";
import { ref, reactive, onMounted } from 'vue';
import { ElMessage } from 'element-plus';
import { Search, Refresh } from '@element-plus/icons-vue';

const queryFormRef = ref();
const loading = ref(false);
const total = ref(0);

const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  username: '',
  mobile: '',
});

const pageData = ref([]);

function handleQuery() {
  loading.value = true;
  RechargeAPI.getMyPage(queryParams)
    .then((data: any) => {
      pageData.value = data.list;
      total.value = data.total;
    })
    .finally(() => {
      loading.value = false;
    });
}

function handleResetQuery() {
  queryFormRef.value?.resetFields();
  queryParams.pageNum = 1;
  handleQuery();
}

function getOperationTypeText(type: number) {
  switch (type) {
    case 1: return '支付宝';
    case 2: return '微信';
    case 3: return '后台充值';
    case 4: return '线下充值';
    default: return '其他';
  }
}

function getAuditStatusText(status: number) {
  switch (status) {
    case 0: return '待审核';
    case 1: return '审核通过';
    case 2: return '审核拒绝';
    default: return '无需审核';
  }
}

function getAuditStatusType(status: number) {
  switch (status) {
    case 0: return 'warning';
    case 1: return 'success';
    case 2: return 'danger';
    default: return '';
  }
}

onMounted(() => {
  handleQuery();
});
</script>
